Chapter 7: Strings
From book Python Programming (Problem solving, Packages and Libraries) published by McGraw Hill Education (India) Private limited. By:

  • Anurag Gupta
  • G. P. Biswas

Note the following:-

  1. This html document is meant as an accompaniment to Chapter 7 Strings .
  2. The document contains scripts executed on IDLE as well as on Jupyter notebook.
  3. The scripts executed on Jupyter can be directly copied and run into a Jupyter notebook or some other IDE (Like Pycharm or Eclipse with PyDev or Visual studio).
  4. However the scripts on IDLE also contain the >>> symbol and therefore cannot be directly executed. If you want to execute them on IDLE or Jupyter, you need to manually remove the >>> symbol.
  5. Wherever needed some background material from the book is also included to help you better understand the scripts
  6. The topic numbers given on each paragraph match the topic numbers of the book, so you can easily identify the topics and corresponding scripts.
  7. In some of the scripts, the file paths give are that of the author's computer. You need to replace them with file paths of your own computer.
  8. At some places, to improve readability, page numbers of the book are indicated in green font like:- See Page 181 of the book
  9. This document was first created as a Jupyter Notebook as combination of Markdown and code cells (extension .ipynb) and then downloaded as html. If someone wants to "modify" or "extend' this document, you may ask for the original .ipynb file by sending me an e-mail at:- 999.anuraggupta@gmail.com

7.2. Creating, initializing and accessing elements of a string
7.2.1 Creating strings

You can create a string in Python in a number of different ways. Some such ways are shown as follows: (i) Using single quotes
A literal string can be created using single quotes, such as ‘abc’. If a string is created using single quotes, it can include double quotes inside. Hence, ‘a”b”c’ is a valid string in Python. An empty or null string is created by ‘’. On IDLE, it will appear as follows:

# ---ON IDLE--- 
>>> myStr = 'abc'
>>> myStr
'abc'
>>> myStr2 = 'a"b"c'
>>> myStr2
'a"b"c'
>>>

(iii) Using “triple single” or “triple double” quotes
Strings can also be created using triple quotes (single or double) when you need to span the string on multiple lines. On IDLE:

# ---ON IDLE--- 
>>> '''abc'''           # Triple single quoted string on single line
'abc'
>>> """abc"""       # Triple double quoted string on single line
'abc'
>>> '''ab           # Triple single quoted string on multiple lines
c'''
'ab\nc'
>>> """ab           # Triple double quoted string on multiple lines
c"""
'ab\nc'

(iv) Using the str(object) builtin function
As explained earlier, everything in Python is an object. So data types, such has integers, floats, lists, tuples, and so on are all objects. These objects have a “string representation”. Without going into too muchdetail, it is sufficient to understand that if an object is given as an argument to a str(object) function, it will convert it into a string. If no argument is given, for instance, str(), then an empty string is created. On IDLE:

# ---ON IDLE--- 
>>> str(1)    #integer 1 converted to string
'1'
>>> str([1,2,'a'])  #List converted to string
"[1, 2, 'a']"
>>> str()    # Empty string created
''
>>>

7.2.2 String indexing
Strings in Python are ordered collections of characters. “Ordered” means that it is a sequence of characters. In Python, the individual characters are accessed by their index, which starts from 0 and ends at one less than the length of the string. Thus, if the length of a string is n, then its index will be from 0 to n-1. (This is similar to C++). However, in Python, you can access the individual characters of a string using negative index also. The last character in the string has a negative index of -1 and the first character (for a string of n characters ) has an index of –n. The indexing applies to string variables as well as string literals. This is clear from the following:

In [1]:
myStr = 'Hello World!'
print('Character at index 0->', myStr[0])
# You can also use indexing on a string Literal
print('Hello World'[0])     # Accessing character at index 0 of a literal string
Character at index 0-> H
H

The index of a string starts from 0. In fact, all indexes in all sequences in Python start from 0. The negative index starts from -1 as shown in Figure 1.1 and -1 is the index of the last character in the string. The other important characteristic of a string sequence is its immutability, which means you cannot change any character appearing at a particular index (position) of the string.

# ---ON IDLE--- 
>>> myString = "Hello World!"
>>> myString[0]   # Index 0 is the first character in the string
'H'
>>> myString[-1]   # Index -1 is the last character in the string
'!'
>>> myString[15]  # myString has 12 characters so index can only be from 0 to 11 or -1 to -12
Traceback (most recent call last):
  File "<pyshell#3>", line 1, in<module>
    myString[15]
IndexError: string index out of range
>>> myString[0] = '0'#Error because strings are immutable
Traceback (most recent call last):
  File "<pyshell#4>", line 1, in<module>
    myString[0] = '0'
TypeError: 'str' object does not support item assignment

An example that shows how to access the individual characters of a literal string is as follows:

# ---ON IDLE--- 
>>>'cat'[0] # Syntax OK
'c'
>>>'cat'.[0] # Syntax not OK since dot ie '.' not allowed
SyntaxError: invalid syntax

7.2.3 Special character and escape sequenc
Some common escape sequences of both types are given in Table 1.1.of the book.
There use will be clear from the following on IDLE:

# ---ON IDLE--- 
>>> s1 = 'abc\ndef'# \n will act as a new line
>>>print(s1)
abc
def
>>>print('abc\tdef')   # \t will act like a tab
abc    def
>>>print('abc\\\n\\def')   #\\\n will act like a \ followed by a new line
abc\
\def
>>>

7.3.1 Traversing a string using ‘for’ loop
You can traverse or iterate over a string using a ‘for’ loop as follows:

In [2]:
myStr = 'ABCD'
for iChar in myStr:
    print(iChar)
A
B
C
D

7.3.2 Traversing a string using while loop
You can loop over a string using the ‘while’ loop. The number of iterations will depend upon the length of the string, which can be found using the len(string) function as follows:

In [3]:
myStr = 'ABCDEF'
myCount = len(myStr)
iCount = 0
while iCount < myCount:
    print('At index ',iCount, '->',myStr[iCount])
    iCount = iCount +1
At index  0 -> A
At index  1 -> B
At index  2 -> C
At index  3 -> D
At index  4 -> E
At index  5 -> F

7.3.3 Traversing a string using ‘for’ loop with the range() function
You can traverse a string using range() also, as follows:

In [4]:
myStr = 'ABCDEF'
myCount = len(myStr)
iCount = 0
for iCount in range(myCount):
    print('At index ',iCount, '->',myStr[iCount])
    iCount = iCount +1
At index  0 -> A
At index  1 -> B
At index  2 -> C
At index  3 -> D
At index  4 -> E
At index  5 -> F

7.4 String Operations
7.4.1 The plus, that is, ‘+ ’ operator
In Python, the ‘+’ operator can be used to concatenate two strings. The following script prints the odd and even characters of a string input by the user:

In [5]:
myS = input("Give a string ")
L = len(myS)
S1 = S2 = '' # S1 and S2 will hold odd and even characters respectively
for x in range(L):
    if x % 2 == 0:
        S1 = S1 + myS[x]
    else:
        S2 = S2 + myS[x]
print('Odd characters-> ',S1, ' Even characters-> ', S2)    
Give a string abcdefghijkl12345678
Odd characters->  acegik1357  Even characters->  bdfhjl2468

7.4.2 The multiplication operator ‘ *’
In Python, the multiply, that is, ‘*’ operator can be used to multiply a string with an integer. But you cannot multiply two strings.
The following code shows how you can multiply a string with a number to generate a pyramid of numbers:

In [6]:
for x in range(5):
    y = x+1 # start the count from 1 not 0
    print(y * str(y)) # str(y) will cast y to a string)
1
22
333
4444
55555

7.4.3 Operators “in” and “not in”
Python has two membership operators, namely “in” and “not in”. These two membership operators can be used to test whether a particular value/ variable/ data exists (or doesn’t exist) in a sequence (that is, string, list, tuple, set and dictionary). The “in” and “not in” operators generate a “boolean context”, that is, the expression using “in” and “not in” will generate either a “True” or a “False”.
This will be clear from the following:

# ---ON IDLE--- 
>>>'a' in 'abc'
True
>>>'d' in 'abc'
False
>>>'d' not in 'abc'
True
>>>

7.4.4 String slicing
The slice operation on IDLE is as follows:

# ---ON IDLE--- 
>>> myStr = 'Hello World!'
>>> mySlice = myStr[1:6]    # Assigning a string slice to a new string variable
>>> mySlice
'ello '
>>>'Hello World!'[1:6]        # Slicing a string literal
'ello '
>>> myStr[-11:-7]   # Can use negative index to slice string also
'ello'

The basics of slicing can be summarized as follows:

  • A string (which is a sequence object), can be sliced using a pair of indices in square brackets and separated by a colon.

  • The index on the left is lower bound and is included in the slice.

  • The index on the right is the upper bound and is excluded from the slice.

  • Slicing creates a new object, that is, a new string.

  • Further, you can exclude (writing in square brackets) either the upper bound or the lower bound or both. o If lower bound is excluded, it defaults to 0. (For example myStr[:6] is same as myStr[0:6]) o If upper bound is excluded it defaults to the index of the last character of the string (Which is also -1). For example myStr[1:] is same as myStr[1:-1]

  • If both lower and upper bound are excluded, it copies the entire string and a new string which is the same as the original, is created.
# ---ON IDLE--- 
>>> myStr = 'Hello World!'
>>> myStr[:6]    #Lower bound excluded so lower bound defaults to 0
'Hello '
>>> myStr[6:]    # Upper bound excluded so upper bound defaults to end of string
'World!'
>>> myStr[6:-1]     #Same as myStr[6:11]
'World'
>>> myStrCopy = myStr[:]    # Both bounds excluded, creates copy
>>> myStrCopy
'Hello World!'
>>> myStr[:-1]      # Entire string excluding last character
'Hello World'

7.4.5 String extended slicing
Extended slice is of format myStr[x: y: z]. x is start index, y is stop index and z is the step or stride.
Any of the three can be excluded. If z is excluded it defaults to +1. As before, if x is excluded, it defaults to 0 and if y is excluded, it defaults to index of the last character of the string.
Note that as before, the character at index x is ALWAYS included and the character at index y is NEVER included.
Consider the following example:-

# ---ON IDLE--- 
>>> myLetters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
>>> myLetters[1:25:2]
'BDFHJLNPRTVX'

You can also use a negative stride, that is, -1 to reverse the order of the character of a stringm, as follows:

# ---ON IDLE--- 
>>> myLetters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
>>> myLettersRev = myLetters[:: -1] # Creates a new string which is reverse of old
>>> myLettersRev
'ZYXWVUTSRQPONMLKJIHGFEDCBA'

7.4.6 Comparison of strings using relational operators
As explained earlier, comparison generates Booleans.
You can compare not only number “types”, such as int and float, but even strings.
Text comparisons are based on ordering of characters in the Unicode set. For instance, in the English language, the unicodes for uppercase come before lowercase (that is, uppercase precedes lowercase).
Hence, a string beginning with an uppercase letter will be “smaller” than a string beginning with a lower case letter.

# ---ON IDLE--- 
>>>'boy'<'girl'  # Alphabetic order
True
>>>'Boy'<'boy'  # Uppercase before lowercase
True
>>>'Girl'<'boy'  # All uppercase before lowercase so G before b
True

`7.6 String functions versus string methods
7.6.1 str.capitalize() where str is a string
Return a copy of the string with its first character capitalized and the rest lowercased.

# ---ON IDLE--- 
>>> str = 'abc'
>>> str.capitalize()
'Abc'
>>> str1 = '?abc'# If first letter non-alphabet, then does nothing
>>> str1.capitalize()
'?abc'

7.6.2 str.count(sub[, start[, stop]])
Remember, sub is the substring being searched in the string str. The method count() takes a substring as a parameter. It then checks and returns the number of occurrences of the “sub-string” in the string.
Note that the parameter substring given to the count method also has two optional parameters, namely “start” and “stop”.
Therefore, if you don’t give these two “optional parameters, that is, start and stop” then the entire substring is checked within the string. With the count() method, you get the “number of occurrences” of the given substring “sub” in the range [start, stop]. Optional arguments “start” and “stop” have the same meaning as they do in “slice notation”.
The following examples will clarify the concept:

# ---ON IDLE--- 
>>> s1 = '01234'
>>> s1.count('0')# Here ‘0’ is the substring being searched in string s1
1
>>> s1.count('0', 1)   # count will start from index 1, hence count is 0
0
>>> s1.count('4', 0, 5)    #length of string is 5, so the last
#character is considered if 5 is last parameter
1
>>> str.count('4', 0, 4)    # last param is 4 so character at index 4
# ie '4' is not counted
0

7.6.2.1 str.find(pat[, start[, stop]])
This method gives the starting index in the string where substring “pat” is present, such that “pat” occurs in the slice pat[start: stop].
The optional arguments “start” and “stop” are interpreted as in slice notation.
The method returns -1 if “pat” is not found.

# ---ON IDLE--- 
>>> str1 = 'abc'
>>> str2 = '01234abc89'
>>> str2.find(str1)
5
>>> str2.find(str1,5)# substring ‘abc starts at index. Final also begins at idex5
5
>>> str2.find(str1,6)# Not found 
-1

The find() method provides only the position of a “pat”, that is, a pattern in a string, that is, the index of occurrence of the sub-string.
If you don’t need the index of the sub-string in the string and simply want to know if the sub-string is present in the string, then use the ‘in’ operator instead, as shown:
To check if pat is a substring or not, use the ‘in’ operator:

# ---ON IDLE--- 
>>>'abc' in 'defabcghi'
True

7.6.2.2 str.isalnum()
The method isalnum() is used to check if all characters in a string are alphanumeric or not.
The method returns True if all characters in the string are alphanumeric and the string “str” has at least one character (that is, it is not an empty string), False otherwise. (Note that methods which begin with ‘is’ like isalnum() generally return a bool

# ---ON IDLE--- 
>>>'abcd12345'.isalnum()
True
>>>'abc 123'.isalnum()# Contains a space which is not alphanumeric
False
>>>'@123asd'.isalnum()
False

7.6.2.3 str.isalpha()
This method returns True if all characters in the string “str” are alphabetic and it has at least one character, and False otherwise.

# ---ON IDLE--- 
>>>'abcd'.isalpha()
True
>>>''.isalpha()# Empty string so ‘’.isalpha() returns False
False
>>>'12sa'.isalpha()
False

1. str.isdigit()
This method returns True if all characters in the string “str” are digits and it has at least one character, which is False otherwise.

# ---ON IDLE--- 
>>>'abc123'.isdigit()  #Since there are alphabet so False
False
>>>'123  '.isdigit()   #Digits with blank space also False
False
>>>'123'.isdigit()     # Only digits without white space True
True

7.6.3 Checking for a “palindrome”
7.6.3.1 Method 1 to check for a palindrome`

In [7]:
def is_Pal(in_string):
    print("String given-> ", in_string)
    iter_var = len(in_string) // 2
    for iVar in range(iter_var):
        print('Iteration number->', iVar)
        if in_string[iVar] != in_string[-(iVar + 1)]:
            print("Alphabet",in_string[iVar],'Didn’t match',in_string[-(iVar +1)])
            return False
        print(in_string[iVar]," matched ",in_string[-(iVar + 1)])
    return True
# Test 'abcdcba'
checkPal = is_Pal('abcdcba')            
print('"abcdcba" is palindrome-> ',checkPal)
print('****************')
# Test 'abcdxcba'
checkPal = is_Pal('abcdxcba')            
print('"abcdxcba" is palindrome?  ',checkPal)
String given->  abcdcba
Iteration number-> 0
a  matched  a
Iteration number-> 1
b  matched  b
Iteration number-> 2
c  matched  c
"abcdcba" is palindrome->  True
****************
String given->  abcdxcba
Iteration number-> 0
a  matched  a
Iteration number-> 1
b  matched  b
Iteration number-> 2
c  matched  c
Iteration number-> 3
Alphabet d Didn’t match x
"abcdxcba" is palindrome?   False

7.6.3.2 Method 2 to check for a palindrome
Another way to check for a palindrome word is to generate a string in reverse and then compare it to the original.
A string can be reversed using the extended slicing with a negative step.
This is shown in the following script:

In [8]:
myStr = input('Give a palindrome->')
print('You gave-> ', myStr)
myStrRev = myStr[::-1]  #  This creates reverse of the input string
print('The reverse of your input is -> ', myStrRev)
if myStr == myStrRev:
    print(myStr, ' is a Palindrome')
else:
    print(myStr, ' is not a Palindrome')
Give a palindrome->asdfghgfdsa
You gave->  asdfghgfdsa
The reverse of your input is ->  asdfghgfdsa
asdfghgfdsa  is a Palindrome

7.7 A short note on string module
Note that the function str() is different from the string module in Python.
In Python, the string is a module, which needs to be imported using the import statement.
In fact, with Python 3.x you don’t need to import the string module since most of the functionalities provided by this module are already available in the string type.
However, the string module has a number of built-in constants, which can be used.

# ---ON IDLE--- 
>>>import string
>>> string.ascii_uppercase      # Gives uppercase letters
'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
>>> string.ascii_lowercase      # Gives lowercase letters
'abcdefghijklmnopqrstuvwxyz'
>>> string.ascii_letters        # Gives all alphabet both upper and lower
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
>>> string.digits               # Gives all digits
'0123456789'
>>> string.hexdigits            # Gives all hex digits ie 0-9 and A-F
'0123456789abcdefABCDEF'
>>> string.octdigits            # Gives all oct digits ie 0-7
'01234567'
>>> string.punctuation          # Gives all punctuation marks
'!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~'
>>> string.whitespace           # Gives all white spaces including tabs and newline
' \t\n\r\x0b\x0c'
>>>

One question that arises is, why do you need such sets of constants in the string module? The following code shows how you can use these string constants:

In [9]:
import string
myS = input("Give a string ")
for c in myS:
    if c in string.ascii_letters:
        print(c, "  is a letter")
    elif c in string.digits:
        print(c, "  is a digit")
    elif c in string.whitespace:
        print(c, "  is a whitespace")
    elif c in string.punctuation:
        print(c, "  is a punctuation mark")
    else:
        print(c, "  is something else")
Give a string a$1w
a   is a letter
$   is a punctuation mark
1   is a digit
w   is a letter

Beyond text book
1. Python has a module calendar which provides useful methods for working with a calendar.
An example of how this module can be used to print a calendar for March 2019, is as follows:

In [10]:
import calendar
# year = 2019, month = 3
print(calendar.month(2019, 3))
     March 2019
Mo Tu We Th Fr Sa Su
             1  2  3
 4  5  6  7  8  9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31

2. Using datetime module
(This exercise can be better done after reading OOP concepts).
Python also has a datetime module. This module provides classes and methods for manipulating dates and times.
The datetime.datetime class returns a datetime object.
The following code shows the use of some common methods of the datetime module:

In [11]:
import datetime
# You can create a datetime object but must give at least YYYY-MM-DD
some_time = datetime.datetime(2019, 1, 1)
print('some_time 1st jan 2019->', some_time)
# datetime.datetime.now for current date-time
now_time = datetime.datetime.now()
print('now_time ->', now_time) # Format is YYYY-MM-DD HH:mm:ss.ssssss
# ISO 8601 is an international standard for representing dates and times
print('isoformat->', now_time.isoformat())
# datetime object has a strftime() method to format time in particular way
# If you want %d/%m/%Y then
print('strftime->', now_time.strftime("%d/%m/%Y"))
# strftime() method is used to create a datetime object from a string
print('strftime with tz->', now_time.strftime("%Y-%m-%d %H:%M:%S %Z%z"))
# For details on strftime() and strptime() Behavior see:-
# https://docs.python.org/3.4/library/datetime.html#strftime-and-strptime-behavior
some_time 1st jan 2019-> 2019-01-01 00:00:00
now_time -> 2019-09-09 15:14:41.244839
isoformat-> 2019-09-09T15:14:41.244839
strftime-> 09/09/2019
strftime with tz-> 2019-09-09 15:14:41 

3. Naïve vs aware datetime objects. (Attempt only after reading OOP concepts)
A “naive” datetime object has no time-zone “awareness”.
The easiest way to check whether a datetime object is “aware” or “naive” is to check for an attribute called “tzinfo” of the datetime object.
If the datetime object is naïve, then this attribute will be None as shown:-

In [12]:
import datetime
obj_naive = datetime.datetime.now()
print(obj_naive.tzinfo)
None

Python has a pytz module, which brings in the “Olson database” into Python and thereby you can make a “naive” object into an “aware” object. The following about this module are relevant:

  • It has an attribute all_timezones_set, which contains all the time zones available in the data base. You can see the contents of this attribute as follows:
In [13]:
import pytz
print(pytz.all_timezones_set)
LazySet({'Etc/GMT', 'Europe/Moscow', 'US/Pacific-New', 'Europe/Astrakhan', 'America/Argentina/Ushuaia', 'Pacific/Yap', 'Canada/Pacific', 'Pacific/Apia', 'Africa/Niamey', 'Europe/Prague', 'Europe/Podgorica', 'Africa/Timbuktu', 'Etc/GMT-12', 'HST', 'Etc/GMT-14', 'Africa/Kigali', 'Indian/Antananarivo', 'America/Chihuahua', 'Atlantic/South_Georgia', 'Australia/ACT', 'Australia/Yancowinna', 'America/Matamoros', 'Africa/Lubumbashi', 'America/North_Dakota/Center', 'US/Pacific', 'Etc/GMT+11', 'Africa/Casablanca', 'Indian/Mahe', 'Australia/Queensland', 'Europe/Isle_of_Man', 'Europe/Malta', 'America/Atka', 'Indian/Christmas', 'America/Yellowknife', 'Asia/Phnom_Penh', 'Europe/Madrid', 'America/Kentucky/Louisville', 'Africa/Addis_Ababa', 'Asia/Barnaul', 'America/Menominee', 'Asia/Bahrain', 'America/Rosario', 'Asia/Kabul', 'Canada/Eastern', 'US/Alaska', 'Asia/Kuching', 'Asia/Istanbul', 'Hongkong', 'Australia/South', 'Europe/Tiraspol', 'Greenwich', 'Africa/Blantyre', 'America/Argentina/San_Luis', 'Asia/Chongqing', 'America/Kralendijk', 'Asia/Harbin', 'Indian/Comoro', 'Africa/Accra', 'America/Pangnirtung', 'Asia/Gaza', 'America/Sao_Paulo', 'America/St_Kitts', 'Asia/Ashkhabad', 'Europe/Saratov', 'Europe/Paris', 'Pacific/Wake', 'Canada/Atlantic', 'Africa/Kinshasa', 'Asia/Katmandu', 'Asia/Saigon', 'America/Marigot', 'America/Goose_Bay', 'America/Mazatlan', 'Arctic/Longyearbyen', 'ROK', 'Europe/Helsinki', 'Europe/Andorra', 'America/Argentina/Mendoza', 'America/Rio_Branco', 'Asia/Urumqi', 'Etc/GMT-4', 'GMT+0', 'America/Asuncion', 'Asia/Colombo', 'America/Cayenne', 'GB', 'Europe/Gibraltar', 'Pacific/Pohnpei', 'Antarctica/Macquarie', 'Zulu', 'Mexico/BajaSur', 'Antarctica/Vostok', 'Africa/Lome', 'Pacific/Fiji', 'CST6CDT', 'America/Bahia', 'Navajo', 'America/Montevideo', 'Asia/Chungking', 'Asia/Tehran', 'America/Manaus', 'Asia/Ulan_Bator', 'America/Tijuana', 'Etc/GMT+1', 'Asia/Ulaanbaatar', 'Etc/GMT+2', 'Kwajalein', 'America/Indiana/Knox', 'America/Grenada', 'Indian/Kerguelen', 'Iceland', 'Africa/Johannesburg', 'EST5EDT', 'America/Rainy_River', 'Pacific/Port_Moresby', 'Europe/Zurich', 'PST8PDT', 'Pacific/Johnston', 'Africa/Conakry', 'Chile/EasterIsland', 'Asia/Kashgar', 'Europe/Budapest', 'Etc/Greenwich', 'Europe/Belgrade', 'NZ', 'Asia/Vientiane', 'America/Argentina/La_Rioja', 'Pacific/Majuro', 'Pacific/Rarotonga', 'Asia/Samarkand', 'Asia/Kathmandu', 'Africa/Windhoek', 'Europe/Riga', 'Asia/Muscat', 'Pacific/Wallis', 'Etc/GMT-5', 'Europe/Kaliningrad', 'Brazil/Acre', 'America/Antigua', 'Asia/Irkutsk', 'Brazil/DeNoronha', 'America/St_Lucia', 'Pacific/Tongatapu', 'Africa/Asmara', 'UTC', 'Asia/Ujung_Pandang', 'Atlantic/Azores', 'Asia/Beirut', 'Universal', 'Etc/GMT-11', 'America/Jujuy', 'Africa/Libreville', 'America/Hermosillo', 'Pacific/Gambier', 'Etc/GMT-6', 'America/Argentina/Jujuy', 'Europe/Vienna', 'Europe/Stockholm', 'Australia/Darwin', 'Europe/Simferopol', 'Europe/Skopje', 'Europe/Nicosia', 'America/Recife', 'Antarctica/Troll', 'Europe/Kiev', 'Africa/Malabo', 'Australia/North', 'America/Tegucigalpa', 'Asia/Kuwait', 'Africa/Gaborone', 'Europe/Monaco', 'America/Thule', 'Asia/Yakutsk', 'ROC', 'Pacific/Ponape', 'America/North_Dakota/Beulah', 'America/Bahia_Banderas', 'America/Whitehorse', 'Etc/GMT+12', 'Asia/Bishkek', 'Asia/Yekaterinburg', 'US/Central', 'GMT0', 'Etc/GMT-10', 'America/Argentina/Rio_Gallegos', 'America/Lima', 'Australia/Canberra', 'America/St_Barthelemy', 'Atlantic/Jan_Mayen', 'Europe/Dublin', 'US/East-Indiana', 'Etc/GMT-3', 'America/Cordoba', 'US/Michigan', 'Etc/GMT+7', 'Europe/Berlin', 'Africa/Maputo', 'Asia/Srednekolymsk', 'Atlantic/Cape_Verde', 'Canada/Newfoundland', 'Cuba', 'Australia/Currie', 'Africa/El_Aaiun', 'Australia/Brisbane', 'America/Belize', 'Asia/Atyrau', 'WET', 'America/Montserrat', 'America/Mexico_City', 'US/Aleutian', 'America/Santiago', 'Asia/Calcutta', 'Pacific/Tahiti', 'America/Indiana/Vevay', 'Africa/Banjul', 'Antarctica/Casey', 'America/Chicago', 'America/Argentina/ComodRivadavia', 'Europe/Kirov', 'Asia/Damascus', 'US/Samoa', 'America/Merida', 'America/St_Thomas', 'Asia/Aqtobe', 'Etc/GMT+4', 'Asia/Dubai', 'Asia/Dhaka', 'Asia/Omsk', 'Africa/Khartoum', 'Pacific/Auckland', 'Antarctica/South_Pole', 'Europe/Sarajevo', 'Asia/Magadan', 'Pacific/Bougainville', 'Africa/Maseru', 'Atlantic/Reykjavik', 'Asia/Novosibirsk', 'Asia/Singapore', 'Atlantic/Faroe', 'America/Indiana/Petersburg', 'Atlantic/St_Helena', 'Africa/Bissau', 'Australia/West', 'Etc/UCT', 'America/Cancun', 'MET', 'Etc/GMT-1', 'America/Metlakatla', 'America/Punta_Arenas', 'America/Montreal', 'Indian/Maldives', 'Pacific/Noumea', 'Europe/Zagreb', 'Africa/Lusaka', 'Europe/Chisinau', 'Chile/Continental', 'America/Boise', 'America/Barbados', 'Europe/Bucharest', 'America/Paramaribo', 'Asia/Vladivostok', 'Asia/Pyongyang', 'Australia/Sydney', 'Asia/Macao', 'America/Nipigon', 'America/Louisville', 'Asia/Thimbu', 'GMT', 'America/Grand_Turk', 'Canada/Mountain', 'Africa/Nairobi', 'America/Aruba', 'America/Virgin', 'America/Nassau', 'Europe/Vilnius', 'America/Indiana/Indianapolis', 'US/Hawaii', 'Pacific/Fakaofo', 'Asia/Dili', 'US/Mountain', 'Canada/East-Saskatchewan', 'Pacific/Chuuk', 'Asia/Tel_Aviv', 'Atlantic/Madeira', 'Pacific/Midway', 'America/Fort_Wayne', 'Europe/Belfast', 'Asia/Jakarta', 'Portugal', 'America/North_Dakota/New_Salem', 'Singapore', 'Iran', 'Pacific/Norfolk', 'Asia/Seoul', 'Pacific/Kosrae', 'Antarctica/DumontDUrville', 'Asia/Khandyga', 'Asia/Karachi', 'Africa/Ouagadougou', 'America/Ensenada', 'Asia/Baghdad', 'America/Guyana', 'America/Ojinaga', 'America/Dawson_Creek', 'America/Araguaina', 'Africa/Bamako', 'Asia/Famagusta', 'America/Fort_Nelson', 'Europe/Volgograd', 'Africa/Lagos', 'America/Thunder_Bay', 'Pacific/Guam', 'Asia/Novokuznetsk', 'America/Argentina/Salta', 'Canada/Saskatchewan', 'America/Port-au-Prince', 'US/Indiana-Starke', 'America/Curacao', 'America/Martinique', 'Australia/Victoria', 'Pacific/Marquesas', 'Europe/Jersey', 'Europe/Uzhgorod', 'Pacific/Pitcairn', 'Asia/Rangoon', 'Africa/Bujumbura', 'Europe/Oslo', 'Asia/Kolkata', 'America/Resolute', 'MST7MDT', 'Asia/Ho_Chi_Minh', 'Asia/Nicosia', 'Europe/Zaporozhye', 'America/Bogota', 'America/Campo_Grande', 'Africa/Djibouti', 'America/Cayman', 'America/New_York', 'Asia/Jerusalem', 'Asia/Thimphu', 'Asia/Yangon', 'Asia/Hovd', 'America/Rankin_Inlet', 'CET', 'Europe/Brussels', 'Asia/Chita', 'Europe/Luxembourg', 'Africa/Monrovia', 'America/Kentucky/Monticello', 'Africa/Porto-Novo', 'Australia/Melbourne', 'America/Indiana/Tell_City', 'Australia/Lord_Howe', 'Pacific/Efate', 'Brazil/West', 'Africa/Tunis', 'Africa/Sao_Tome', 'America/Dawson', 'Pacific/Tarawa', 'Pacific/Saipan', 'Pacific/Kiritimati', 'America/Guatemala', 'W-SU', 'Asia/Hebron', 'America/Argentina/Tucuman', 'Asia/Pontianak', 'America/Catamarca', 'Antarctica/Syowa', 'America/Indiana/Winamac', 'America/Knox_IN', 'Atlantic/Canary', 'America/Yakutat', 'Europe/Tallinn', 'Indian/Mayotte', 'Africa/Harare', 'Africa/Mbabane', 'Brazil/East', 'Asia/Choibalsan', 'Europe/London', 'Europe/Amsterdam', 'PRC', 'America/Danmarkshavn', 'Asia/Riyadh', 'America/Atikokan', 'Etc/GMT+10', 'America/Shiprock', 'America/Mendoza', 'Asia/Kamchatka', 'Europe/Busingen', 'America/Porto_Acre', 'Asia/Amman', 'Africa/Algiers', 'America/Juneau', 'Europe/Ljubljana', 'America/Tortola', 'Asia/Jayapura', 'Japan', 'Europe/Mariehamn', 'Pacific/Enderbury', 'America/Argentina/Catamarca', 'America/Vancouver', 'America/Panama', 'Etc/GMT-13', 'Australia/NSW', 'Jamaica', 'Etc/GMT-0', 'Africa/Tripoli', 'US/Eastern', 'Etc/GMT-8', 'America/Scoresbysund', 'Asia/Tokyo', 'MST', 'Australia/Adelaide', 'Asia/Hong_Kong', 'America/Eirunepe', 'America/Creston', 'Africa/Luanda', 'America/Santo_Domingo', 'Asia/Yerevan', 'Asia/Krasnoyarsk', 'Australia/Tasmania', 'America/Costa_Rica', 'Etc/GMT+0', 'Etc/Universal', 'Europe/Ulyanovsk', 'Atlantic/Bermuda', 'America/Toronto', 'Etc/UTC', 'America/Anguilla', 'Indian/Chagos', 'Indian/Mauritius', 'Africa/Dakar', 'Africa/Freetown', 'Australia/Broken_Hill', 'America/Caracas', 'Africa/Abidjan', 'Pacific/Galapagos', 'Canada/Central', 'America/Los_Angeles', 'America/Miquelon', 'US/Arizona', 'Europe/Copenhagen', 'Asia/Brunei', 'Egypt', 'Mexico/General', 'America/La_Paz', 'Mexico/BajaNorte', 'America/Monterrey', 'Europe/Istanbul', 'America/Indiana/Marengo', 'America/Phoenix', 'America/Coral_Harbour', 'Asia/Aden', 'Africa/Kampala', 'America/Adak', 'Pacific/Funafuti', 'Etc/GMT0', 'EET', 'America/Santa_Isabel', 'Asia/Baku', 'Asia/Tbilisi', 'Etc/GMT+5', 'Australia/Eucla', 'Antarctica/Rothera', 'Europe/Warsaw', 'Africa/Asmera', 'Turkey', 'Europe/Vaduz', 'America/Indianapolis', 'America/Santarem', 'Asia/Almaty', 'America/Cambridge_Bay', 'Pacific/Nauru', 'Asia/Sakhalin', 'America/Glace_Bay', 'America/Argentina/San_Juan', 'Etc/GMT+9', 'Asia/Kuala_Lumpur', 'America/Boa_Vista', 'Africa/Mogadishu', 'America/Swift_Current', 'GMT-0', 'Africa/Cairo', 'America/Fortaleza', 'Europe/Athens', 'Europe/Vatican', 'Antarctica/Davis', 'America/Moncton', 'Africa/Ndjamena', 'America/Dominica', 'Africa/Douala', 'Australia/Perth', 'Asia/Tashkent', 'Indian/Cocos', 'Pacific/Easter', 'Pacific/Guadalcanal', 'Asia/Tomsk', 'Asia/Macau', 'EST', 'Pacific/Honolulu', 'Asia/Qyzylorda', 'Pacific/Samoa', 'Eire', 'Antarctica/Mawson', 'UCT', 'Pacific/Niue', 'America/St_Vincent', 'America/El_Salvador', 'Asia/Aqtau', 'Atlantic/Faeroe', 'America/Noronha', 'GB-Eire', 'Africa/Bangui', 'America/Denver', 'America/Cuiaba', 'America/Argentina/Buenos_Aires', 'America/Buenos_Aires', 'America/Havana', 'America/Jamaica', 'Etc/GMT+6', 'Antarctica/McMurdo', 'Israel', 'Pacific/Pago_Pago', 'Etc/GMT+3', 'America/Indiana/Vincennes', 'Antarctica/Palmer', 'Atlantic/Stanley', 'Asia/Makassar', 'Pacific/Palau', 'America/Detroit', 'Asia/Qatar', 'America/Porto_Velho', 'Africa/Brazzaville', 'America/Guayaquil', 'Europe/Bratislava', 'Asia/Anadyr', 'America/Winnipeg', 'Europe/Rome', 'NZ-CHAT', 'Australia/Hobart', 'Etc/Zulu', 'America/Blanc-Sablon', 'Canada/Yukon', 'Asia/Shanghai', 'America/Godthab', 'America/Regina', 'Etc/GMT-2', 'Asia/Dacca', 'Africa/Nouakchott', 'America/Argentina/Cordoba', 'America/Guadeloupe', 'Asia/Manila', 'Australia/Lindeman', 'Africa/Dar_es_Salaam', 'America/Port_of_Spain', 'America/Iqaluit', 'Europe/Tirane', 'Libya', 'Asia/Dushanbe', 'Europe/Sofia', 'Asia/Ashgabat', 'America/Sitka', 'America/Belem', 'Europe/Minsk', 'Pacific/Truk', 'Asia/Taipei', 'Australia/LHI', 'Africa/Ceuta', 'Pacific/Kwajalein', 'Poland', 'Pacific/Chatham', 'America/Puerto_Rico', 'America/Halifax', 'America/Lower_Princes', 'America/St_Johns', 'Etc/GMT+8', 'Europe/San_Marino', 'America/Anchorage', 'America/Maceio', 'America/Managua', 'Europe/Guernsey', 'Africa/Juba', 'America/Nome', 'Indian/Reunion', 'America/Inuvik', 'Europe/Samara', 'Etc/GMT-9', 'America/Edmonton', 'Asia/Bangkok', 'Asia/Oral', 'Europe/Lisbon', 'Asia/Ust-Nera', 'Etc/GMT-7'})

So the steps in converting a timezone “aware” object are as follows:

  1. create a timezone “naive” object.
  2. Then create a “timezone” object for the time zone you want to work in.
  3. Use the localize(dt) method of this timezone object to create a timezone “aware” object. Note that the “dt” parameter is the timezone “naive” object you created earlier.

You can create a timezone aware object as shown:

In [14]:
import datetime
import pytz
# The olson database has 'Asia/Kolkata', 'Asia/Calcutta'
# --- Step 1. Create a naive object for current date-time
naive_dt = datetime.datetime.now()
# Its tzinfo is None
print("tzinfo->", naive_dt.tzinfo) # tzinfo-> None
#--- Step 2. Create a timezone object for 'Asia/Kolkata'
tz_kolkata = pytz.timezone('Asia/Kolkata')
#--- Step 3. Use localize(dt) method to create "aware" object
aware_dt = tz_kolkata.localize(naive_dt)
print('aware_dt->', aware_dt.tzinfo)
tzinfo-> None
aware_dt-> Asia/Kolkata

The following code is another example of how the pytz module can be used to make the timezone aware date-time objects:-

In [15]:
import pytz
print('dir(pytz)->', dir(pytz))
# Gives 2 letter code of countries. Have to cast to list
print('Country 2 letter code->', list(pytz.country_names))
# Gives names of common time zones.
print('common timezones->', pytz.common_timezones) 
# India has only 1 time zone 'Asia/Kolkata'
print('timezone for India->', pytz.country_timezones['IN'])
# time-zone aware time for India
now_india = datetime.datetime.now(pytz.timezone('Asia/Kolkata'))
# Confirm that IST is +05:30
print('time zone aware for India->', now_india)
dir(pytz)-> ['AmbiguousTimeError', 'FixedOffset', 'HOUR', 'InvalidTimeError', 'LazyDict', 'LazyList', 'LazySet', 'NonExistentTimeError', 'OLSEN_VERSION', 'OLSON_VERSION', 'UTC', 'UnknownTimeZoneError', 'VERSION', 'ZERO', '_CountryNameDict', '_CountryTimezoneDict', '_FixedOffset', '_UTC', '__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__', '__version__', '_byte_string', '_p', '_test', '_tzinfo_cache', '_unmunge_zone', 'all_timezones', 'all_timezones_set', 'ascii', 'build_tzinfo', 'common_timezones', 'common_timezones_set', 'country_names', 'country_timezones', 'datetime', 'exceptions', 'gettext', 'lazy', 'open_resource', 'os', 'resource_exists', 'sys', 'timezone', 'tzfile', 'tzinfo', 'unicode', 'unpickler', 'utc']
Country 2 letter code-> ['AD', 'AE', 'AF', 'AG', 'AI', 'AL', 'AM', 'AO', 'AQ', 'AR', 'AS', 'AT', 'AU', 'AW', 'AX', 'AZ', 'BA', 'BB', 'BD', 'BE', 'BF', 'BG', 'BH', 'BI', 'BJ', 'BL', 'BM', 'BN', 'BO', 'BQ', 'BR', 'BS', 'BT', 'BV', 'BW', 'BY', 'BZ', 'CA', 'CC', 'CD', 'CF', 'CG', 'CH', 'CI', 'CK', 'CL', 'CM', 'CN', 'CO', 'CR', 'CU', 'CV', 'CW', 'CX', 'CY', 'CZ', 'DE', 'DJ', 'DK', 'DM', 'DO', 'DZ', 'EC', 'EE', 'EG', 'EH', 'ER', 'ES', 'ET', 'FI', 'FJ', 'FK', 'FM', 'FO', 'FR', 'GA', 'GB', 'GD', 'GE', 'GF', 'GG', 'GH', 'GI', 'GL', 'GM', 'GN', 'GP', 'GQ', 'GR', 'GS', 'GT', 'GU', 'GW', 'GY', 'HK', 'HM', 'HN', 'HR', 'HT', 'HU', 'ID', 'IE', 'IL', 'IM', 'IN', 'IO', 'IQ', 'IR', 'IS', 'IT', 'JE', 'JM', 'JO', 'JP', 'KE', 'KG', 'KH', 'KI', 'KM', 'KN', 'KP', 'KR', 'KW', 'KY', 'KZ', 'LA', 'LB', 'LC', 'LI', 'LK', 'LR', 'LS', 'LT', 'LU', 'LV', 'LY', 'MA', 'MC', 'MD', 'ME', 'MF', 'MG', 'MH', 'MK', 'ML', 'MM', 'MN', 'MO', 'MP', 'MQ', 'MR', 'MS', 'MT', 'MU', 'MV', 'MW', 'MX', 'MY', 'MZ', 'NA', 'NC', 'NE', 'NF', 'NG', 'NI', 'NL', 'NO', 'NP', 'NR', 'NU', 'NZ', 'OM', 'PA', 'PE', 'PF', 'PG', 'PH', 'PK', 'PL', 'PM', 'PN', 'PR', 'PS', 'PT', 'PW', 'PY', 'QA', 'RE', 'RO', 'RS', 'RU', 'RW', 'SA', 'SB', 'SC', 'SD', 'SE', 'SG', 'SH', 'SI', 'SJ', 'SK', 'SL', 'SM', 'SN', 'SO', 'SR', 'SS', 'ST', 'SV', 'SX', 'SY', 'SZ', 'TC', 'TD', 'TF', 'TG', 'TH', 'TJ', 'TK', 'TL', 'TM', 'TN', 'TO', 'TR', 'TT', 'TV', 'TW', 'TZ', 'UA', 'UG', 'UM', 'US', 'UY', 'UZ', 'VA', 'VC', 'VE', 'VG', 'VI', 'VN', 'VU', 'WF', 'WS', 'YE', 'YT', 'ZA', 'ZM', 'ZW']
common timezones-> ['Africa/Abidjan', 'Africa/Accra', 'Africa/Addis_Ababa', 'Africa/Algiers', 'Africa/Asmara', 'Africa/Bamako', 'Africa/Bangui', 'Africa/Banjul', 'Africa/Bissau', 'Africa/Blantyre', 'Africa/Brazzaville', 'Africa/Bujumbura', 'Africa/Cairo', 'Africa/Casablanca', 'Africa/Ceuta', 'Africa/Conakry', 'Africa/Dakar', 'Africa/Dar_es_Salaam', 'Africa/Djibouti', 'Africa/Douala', 'Africa/El_Aaiun', 'Africa/Freetown', 'Africa/Gaborone', 'Africa/Harare', 'Africa/Johannesburg', 'Africa/Juba', 'Africa/Kampala', 'Africa/Khartoum', 'Africa/Kigali', 'Africa/Kinshasa', 'Africa/Lagos', 'Africa/Libreville', 'Africa/Lome', 'Africa/Luanda', 'Africa/Lubumbashi', 'Africa/Lusaka', 'Africa/Malabo', 'Africa/Maputo', 'Africa/Maseru', 'Africa/Mbabane', 'Africa/Mogadishu', 'Africa/Monrovia', 'Africa/Nairobi', 'Africa/Ndjamena', 'Africa/Niamey', 'Africa/Nouakchott', 'Africa/Ouagadougou', 'Africa/Porto-Novo', 'Africa/Sao_Tome', 'Africa/Tripoli', 'Africa/Tunis', 'Africa/Windhoek', 'America/Adak', 'America/Anchorage', 'America/Anguilla', 'America/Antigua', 'America/Araguaina', 'America/Argentina/Buenos_Aires', 'America/Argentina/Catamarca', 'America/Argentina/Cordoba', 'America/Argentina/Jujuy', 'America/Argentina/La_Rioja', 'America/Argentina/Mendoza', 'America/Argentina/Rio_Gallegos', 'America/Argentina/Salta', 'America/Argentina/San_Juan', 'America/Argentina/San_Luis', 'America/Argentina/Tucuman', 'America/Argentina/Ushuaia', 'America/Aruba', 'America/Asuncion', 'America/Atikokan', 'America/Bahia', 'America/Bahia_Banderas', 'America/Barbados', 'America/Belem', 'America/Belize', 'America/Blanc-Sablon', 'America/Boa_Vista', 'America/Bogota', 'America/Boise', 'America/Cambridge_Bay', 'America/Campo_Grande', 'America/Cancun', 'America/Caracas', 'America/Cayenne', 'America/Cayman', 'America/Chicago', 'America/Chihuahua', 'America/Costa_Rica', 'America/Creston', 'America/Cuiaba', 'America/Curacao', 'America/Danmarkshavn', 'America/Dawson', 'America/Dawson_Creek', 'America/Denver', 'America/Detroit', 'America/Dominica', 'America/Edmonton', 'America/Eirunepe', 'America/El_Salvador', 'America/Fort_Nelson', 'America/Fortaleza', 'America/Glace_Bay', 'America/Godthab', 'America/Goose_Bay', 'America/Grand_Turk', 'America/Grenada', 'America/Guadeloupe', 'America/Guatemala', 'America/Guayaquil', 'America/Guyana', 'America/Halifax', 'America/Havana', 'America/Hermosillo', 'America/Indiana/Indianapolis', 'America/Indiana/Knox', 'America/Indiana/Marengo', 'America/Indiana/Petersburg', 'America/Indiana/Tell_City', 'America/Indiana/Vevay', 'America/Indiana/Vincennes', 'America/Indiana/Winamac', 'America/Inuvik', 'America/Iqaluit', 'America/Jamaica', 'America/Juneau', 'America/Kentucky/Louisville', 'America/Kentucky/Monticello', 'America/Kralendijk', 'America/La_Paz', 'America/Lima', 'America/Los_Angeles', 'America/Lower_Princes', 'America/Maceio', 'America/Managua', 'America/Manaus', 'America/Marigot', 'America/Martinique', 'America/Matamoros', 'America/Mazatlan', 'America/Menominee', 'America/Merida', 'America/Metlakatla', 'America/Mexico_City', 'America/Miquelon', 'America/Moncton', 'America/Monterrey', 'America/Montevideo', 'America/Montserrat', 'America/Nassau', 'America/New_York', 'America/Nipigon', 'America/Nome', 'America/Noronha', 'America/North_Dakota/Beulah', 'America/North_Dakota/Center', 'America/North_Dakota/New_Salem', 'America/Ojinaga', 'America/Panama', 'America/Pangnirtung', 'America/Paramaribo', 'America/Phoenix', 'America/Port-au-Prince', 'America/Port_of_Spain', 'America/Porto_Velho', 'America/Puerto_Rico', 'America/Punta_Arenas', 'America/Rainy_River', 'America/Rankin_Inlet', 'America/Recife', 'America/Regina', 'America/Resolute', 'America/Rio_Branco', 'America/Santarem', 'America/Santiago', 'America/Santo_Domingo', 'America/Sao_Paulo', 'America/Scoresbysund', 'America/Sitka', 'America/St_Barthelemy', 'America/St_Johns', 'America/St_Kitts', 'America/St_Lucia', 'America/St_Thomas', 'America/St_Vincent', 'America/Swift_Current', 'America/Tegucigalpa', 'America/Thule', 'America/Thunder_Bay', 'America/Tijuana', 'America/Toronto', 'America/Tortola', 'America/Vancouver', 'America/Whitehorse', 'America/Winnipeg', 'America/Yakutat', 'America/Yellowknife', 'Antarctica/Casey', 'Antarctica/Davis', 'Antarctica/DumontDUrville', 'Antarctica/Macquarie', 'Antarctica/Mawson', 'Antarctica/McMurdo', 'Antarctica/Palmer', 'Antarctica/Rothera', 'Antarctica/Syowa', 'Antarctica/Troll', 'Antarctica/Vostok', 'Arctic/Longyearbyen', 'Asia/Aden', 'Asia/Almaty', 'Asia/Amman', 'Asia/Anadyr', 'Asia/Aqtau', 'Asia/Aqtobe', 'Asia/Ashgabat', 'Asia/Atyrau', 'Asia/Baghdad', 'Asia/Bahrain', 'Asia/Baku', 'Asia/Bangkok', 'Asia/Barnaul', 'Asia/Beirut', 'Asia/Bishkek', 'Asia/Brunei', 'Asia/Chita', 'Asia/Choibalsan', 'Asia/Colombo', 'Asia/Damascus', 'Asia/Dhaka', 'Asia/Dili', 'Asia/Dubai', 'Asia/Dushanbe', 'Asia/Famagusta', 'Asia/Gaza', 'Asia/Hebron', 'Asia/Ho_Chi_Minh', 'Asia/Hong_Kong', 'Asia/Hovd', 'Asia/Irkutsk', 'Asia/Jakarta', 'Asia/Jayapura', 'Asia/Jerusalem', 'Asia/Kabul', 'Asia/Kamchatka', 'Asia/Karachi', 'Asia/Kathmandu', 'Asia/Khandyga', 'Asia/Kolkata', 'Asia/Krasnoyarsk', 'Asia/Kuala_Lumpur', 'Asia/Kuching', 'Asia/Kuwait', 'Asia/Macau', 'Asia/Magadan', 'Asia/Makassar', 'Asia/Manila', 'Asia/Muscat', 'Asia/Nicosia', 'Asia/Novokuznetsk', 'Asia/Novosibirsk', 'Asia/Omsk', 'Asia/Oral', 'Asia/Phnom_Penh', 'Asia/Pontianak', 'Asia/Pyongyang', 'Asia/Qatar', 'Asia/Qyzylorda', 'Asia/Riyadh', 'Asia/Sakhalin', 'Asia/Samarkand', 'Asia/Seoul', 'Asia/Shanghai', 'Asia/Singapore', 'Asia/Srednekolymsk', 'Asia/Taipei', 'Asia/Tashkent', 'Asia/Tbilisi', 'Asia/Tehran', 'Asia/Thimphu', 'Asia/Tokyo', 'Asia/Tomsk', 'Asia/Ulaanbaatar', 'Asia/Urumqi', 'Asia/Ust-Nera', 'Asia/Vientiane', 'Asia/Vladivostok', 'Asia/Yakutsk', 'Asia/Yangon', 'Asia/Yekaterinburg', 'Asia/Yerevan', 'Atlantic/Azores', 'Atlantic/Bermuda', 'Atlantic/Canary', 'Atlantic/Cape_Verde', 'Atlantic/Faroe', 'Atlantic/Madeira', 'Atlantic/Reykjavik', 'Atlantic/South_Georgia', 'Atlantic/St_Helena', 'Atlantic/Stanley', 'Australia/Adelaide', 'Australia/Brisbane', 'Australia/Broken_Hill', 'Australia/Currie', 'Australia/Darwin', 'Australia/Eucla', 'Australia/Hobart', 'Australia/Lindeman', 'Australia/Lord_Howe', 'Australia/Melbourne', 'Australia/Perth', 'Australia/Sydney', 'Canada/Atlantic', 'Canada/Central', 'Canada/Eastern', 'Canada/Mountain', 'Canada/Newfoundland', 'Canada/Pacific', 'Europe/Amsterdam', 'Europe/Andorra', 'Europe/Astrakhan', 'Europe/Athens', 'Europe/Belgrade', 'Europe/Berlin', 'Europe/Bratislava', 'Europe/Brussels', 'Europe/Bucharest', 'Europe/Budapest', 'Europe/Busingen', 'Europe/Chisinau', 'Europe/Copenhagen', 'Europe/Dublin', 'Europe/Gibraltar', 'Europe/Guernsey', 'Europe/Helsinki', 'Europe/Isle_of_Man', 'Europe/Istanbul', 'Europe/Jersey', 'Europe/Kaliningrad', 'Europe/Kiev', 'Europe/Kirov', 'Europe/Lisbon', 'Europe/Ljubljana', 'Europe/London', 'Europe/Luxembourg', 'Europe/Madrid', 'Europe/Malta', 'Europe/Mariehamn', 'Europe/Minsk', 'Europe/Monaco', 'Europe/Moscow', 'Europe/Oslo', 'Europe/Paris', 'Europe/Podgorica', 'Europe/Prague', 'Europe/Riga', 'Europe/Rome', 'Europe/Samara', 'Europe/San_Marino', 'Europe/Sarajevo', 'Europe/Saratov', 'Europe/Simferopol', 'Europe/Skopje', 'Europe/Sofia', 'Europe/Stockholm', 'Europe/Tallinn', 'Europe/Tirane', 'Europe/Ulyanovsk', 'Europe/Uzhgorod', 'Europe/Vaduz', 'Europe/Vatican', 'Europe/Vienna', 'Europe/Vilnius', 'Europe/Volgograd', 'Europe/Warsaw', 'Europe/Zagreb', 'Europe/Zaporozhye', 'Europe/Zurich', 'GMT', 'Indian/Antananarivo', 'Indian/Chagos', 'Indian/Christmas', 'Indian/Cocos', 'Indian/Comoro', 'Indian/Kerguelen', 'Indian/Mahe', 'Indian/Maldives', 'Indian/Mauritius', 'Indian/Mayotte', 'Indian/Reunion', 'Pacific/Apia', 'Pacific/Auckland', 'Pacific/Bougainville', 'Pacific/Chatham', 'Pacific/Chuuk', 'Pacific/Easter', 'Pacific/Efate', 'Pacific/Enderbury', 'Pacific/Fakaofo', 'Pacific/Fiji', 'Pacific/Funafuti', 'Pacific/Galapagos', 'Pacific/Gambier', 'Pacific/Guadalcanal', 'Pacific/Guam', 'Pacific/Honolulu', 'Pacific/Kiritimati', 'Pacific/Kosrae', 'Pacific/Kwajalein', 'Pacific/Majuro', 'Pacific/Marquesas', 'Pacific/Midway', 'Pacific/Nauru', 'Pacific/Niue', 'Pacific/Norfolk', 'Pacific/Noumea', 'Pacific/Pago_Pago', 'Pacific/Palau', 'Pacific/Pitcairn', 'Pacific/Pohnpei', 'Pacific/Port_Moresby', 'Pacific/Rarotonga', 'Pacific/Saipan', 'Pacific/Tahiti', 'Pacific/Tarawa', 'Pacific/Tongatapu', 'Pacific/Wake', 'Pacific/Wallis', 'US/Alaska', 'US/Arizona', 'US/Central', 'US/Eastern', 'US/Hawaii', 'US/Mountain', 'US/Pacific', 'UTC']
timezone for India-> ['Asia/Kolkata']
time zone aware for India-> 2019-09-09 15:14:41.867874+05:30